import java.io.IOException;

import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;

import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.apache.log4j.Logger;



public class HttpClientUtil {
	public static Logger log = Logger.getLogger(HttpClientUtil.class);	
	public static String getHttp(String url){
	    String responseMsg = "";

	    // 1.构�?�HttpClient的实�?
	    HttpClient httpClient = new HttpClient();

	    // 2.创建GetMethod的实�?
	    GetMethod getMethod = new GetMethod(url);

	    // 使用系统系统的默认的恢复策略
	    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
	            new DefaultHttpMethodRetryHandler());
	   
	    try {
	        //3.执行getMethod,调用http接口
	        httpClient.executeMethod(getMethod);

	        //4.读取内容
	        byte[] responseBody = getMethod.getResponseBody();
	       
	        //5.处理返回的内�?
	        responseMsg = new String(responseBody,"UTF-8");
	        			
	        log.info(responseMsg);
	       
	    } catch (HttpException e) {
	        e.printStackTrace();
	    } catch (IOException e) {
	        e.printStackTrace();
	    }finally{
	        //6.释放连接
	        getMethod.releaseConnection();
	    }
	    return responseMsg;
	}

	/**
	 * post方式
	 * @param param1
	 * @param param2
	 * @return*/

	public static String postHttp(String param1,String param2) {
	    String responseMsg = "";
	   
	    //1.构�?�HttpClient的实�?
	    HttpClient httpClient=new HttpClient();
	   
	    httpClient.getParams().setContentCharset("UTF-8");
	   
	    String url="http://localhost:8080/honghulong/weixinCore.html";
	   
	    //2.构�?�PostMethod的实�?
	    PostMethod postMethod=new PostMethod(url);
	   
	    //3.把参数�?�放入到PostMethod对象�?
	 //方式1�?
	 /*        NameValuePair[] data = { new NameValuePair("param1", param1),
	                 new NameValuePair("param2", param2) };
	         postMethod.setRequestBody(data);*/
	        
	         //方式2�?   
	         postMethod.addParameter("param1", param1);
	         postMethod.addParameter("param2", param2);
	        
	        
	         try {
	             // 4.执行postMethod,调用http接口
	             httpClient.executeMethod(postMethod);//200
	            
	 //5.读取内容
	             responseMsg = postMethod.getResponseBodyAsString().trim();
	             log.info(responseMsg);
	            
	             //6.处理返回的内�?

	         } catch (HttpException e) {
	             e.printStackTrace();
	         } catch (IOException e) {
	             e.printStackTrace();
	         } finally {
	             //7.释放连接
	             postMethod.releaseConnection();
	         }
	         return responseMsg;
	}
	
	/**

	* 根据URL获得所有的html信息

	* 

	* @param url

	* @return

	*/

	
}
